﻿using System;
using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web.Security;
using AzureQueue;
using Microsoft.WindowsAzure.StorageClient;

namespace WebPages.Account
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        void SendConfirmationEmail()
        {
            var userName = Membership.GetUser().UserName;
            var newPassword = ChangeUserPassword.NewPassword;
            var emailAddress = Membership.GetUser().Email;
            var fromEmail = ConfigurationManager.AppSettings["fromEmail"];
            var subject = "Your MyApp account password has been changed";

            // Now lets create an email message
            var emailMessage = new StringBuilder();
            var header = Resources.Email.EmailHeader.Replace("{0}", "Your MyApp account password has been changed");
            emailMessage.Append(header);
            emailMessage.Append(string.Format("Hello, {0}.<br />Your new password is: <span class=\"password\">{1}</span>", userName, newPassword));
            var footer = String.Format(Resources.Email.EmailFooter, "This e-mail was sent because someone changed a password to an account at <a href=\"http://www.MyApp.com\">www.MyApp.com</a> registered to your e-mail address.");
            emailMessage.Append(footer);

            var queueDataSource = new AzureQueueDataSource();
            queueDataSource.SendEmail(fromEmail, emailAddress, subject, emailMessage.ToString());
        }

        protected void ChangeUserPassword_ChangedPassword(object sender, EventArgs e)
        {
            SendConfirmationEmail();
        }
    }
}
